package trabalhotec.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.text.ParseException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.text.MaskFormatter;
import trabalhotec.BD.DataUtil;

public class Promocao extends JInternalFrame {
    JTextField Cod;
    JFormattedTextField Ini;
    JFormattedTextField Fim;
    JTextField Desc;
    JTable promo;
    Vector<String> titulosvoos = new Vector<String>();
    Vector<Vector> valores;
    Vector<String> titulos = new Vector<String>();
    JPanel pane = new JPanel();
    JTable voos;
    trabalhotec.Negocios.Promocao promolist = new trabalhotec.Negocios.Promocao();
    public Promocao(){
       this.setTitle("Promocoes");
       pane = new JPanel();
       pane.setLayout(new BorderLayout());
       titulosvoos.add("Codigo");
       titulosvoos.add("Origem");
       titulosvoos.add("Destino");
       titulosvoos.add("Data de partida");
       titulosvoos.add("Data de chegada");
       titulosvoos.add("Tempo de voo");
       titulosvoos.add("Preco");
       titulosvoos.add("Quantidade de Lugares");
       titulos.add("Codigo");
       titulos.add("Data Inicio");
       titulos.add("Data fim");
       titulos.add("Desconto");
       titulos.add("Voo");
       listarPromo();
       this.setClosable(true);
       this.setMaximizable(false);
       this.setIconifiable(true);
       this.setResizable(false);
       this.getContentPane().add(pane);
    }
    public void listarPromo(){
        pane.removeAll();
        valores = promolist.listarPromo();
        promo = new JTable(valores,titulos);
        JScrollPane panePromo = new JScrollPane(promo);
        pane.setLayout(new BorderLayout());
        JPanel botoes = new JPanel();
        promo.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
        JButton NovaPromo = new JButton("Nova Promocao");
        JButton Sair = new JButton("Sair");
        JButton Excluir = new JButton("Excluir");
        botoes.setLayout(new FlowLayout());
        botoes.add(NovaPromo);
        botoes.add(Sair);
        botoes.add(Excluir);
        NovaPromo.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                cadPromo();
            }   
        });
        Excluir.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                int selRow = promo.getSelectedRow();  
                if(selRow ==-1)JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada");
                else{
                    String valor = (String) promo.getModel().getValueAt(selRow, 0);
                    Object[] options = { "Sim", "Não" };  
                    int i = JOptionPane.showOptionDialog(null,"Tem certeza que deseja excluir?", "Saída", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);  
                    if (i == JOptionPane.YES_OPTION) {  
                        promolist.excluirPromo(valor);
                        JOptionPane.showMessageDialog(null, "Promocao apagado com sucesso");
                        listarPromo();
                    }
                }
            }
            
        });
        Sair.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    setClosed(true);
                } catch (PropertyVetoException ex) {
                    Logger.getLogger(Aeroporto.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        pane.add(panePromo,BorderLayout.CENTER);
        pane.add(botoes,BorderLayout.SOUTH);
        this.pack();
        this.repaint();
        this.revalidate();
    }
    public void cadPromo(){
       pane.removeAll();
       valores = promolist.listarvoos();
       voos = new JTable(valores,titulosvoos);
       voos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       JScrollPane paneVoos = new JScrollPane(voos);
       JLabel codigo = new JLabel("Codigo:");
       JLabel dataini= new JLabel("Data Inicio:");
       JLabel datafim= new JLabel("Data Fim:");
       JLabel desc = new JLabel("Desconto:");
       JLabel codvoo = new JLabel("Codigo do voo:");
       JLabel porcent = new JLabel("%");
       Cod = new JTextField(6);
        try {
            Ini = new JFormattedTextField(new MaskFormatter("##/##/####"));
            Fim = new JFormattedTextField(new MaskFormatter("##/##/####"));
        } catch (ParseException ex) {
            Logger.getLogger(Promocao.class.getName()).log(Level.SEVERE, null, ex);
        }
       Desc = new JTextField(8);
       
       //CodVoo.setText(cod);
       pane.setLayout(new GridBagLayout());
       GridBagConstraints c = new GridBagConstraints();
       c.anchor = GridBagConstraints.FIRST_LINE_START;
       c.gridx=0;
       c.gridy=0;
       pane.add(codigo,c);
       c.gridx=1;
       pane.add(Cod,c);
       c.gridx=0;
       c.gridy=1;
       pane.add(dataini,c);
       c.gridx=1;
       pane.add(Ini,c);
       c.gridx=2;
       pane.add(datafim,c);
       c.gridx=3;
       pane.add(Fim,c);
       c.gridx=0;
       c.gridy=2;
       pane.add(desc,c);
       c.gridx=1;
       pane.add(Desc,c);
       c.gridx=2;
       pane.add(porcent,c);
       c.gridx=0;
       c.gridy=3;
       pane.add(codvoo,c);
       c.gridx=1;
       
       JPanel botoes = new JPanel();
       botoes.setLayout(new FlowLayout());
       JButton Cadastrar = new JButton("Cadastrar");
       JButton LimparDados = new JButton("Limpar Dados");
       JButton Sair = new JButton("Sair");
       botoes.add(Cadastrar);
       botoes.add(LimparDados);
       botoes.add(Sair);
       Cadastrar.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent e) {
               String datainicial = Ini.getText() + " 00:00:00";
               String datafinal = Fim.getText() +" 23:59:59";
               if(DataUtil.validaData(datainicial)&&DataUtil.validaData(datafinal)){
                    if(promolist.validarData(datainicial, datafinal)){
                        int selRow = voos.getSelectedRow();
                        if(selRow == -1)JOptionPane.showMessageDialog(null, "Nenhum voo selecionado");
                        else{
                            String valor = (String) voos.getModel().getValueAt(selRow, 0);
                            promolist.cadPromo(Cod.getText(), datainicial ,datafinal, Float.parseFloat(Desc.getText()), valor);
                            listarPromo();
                        }
                    }else JOptionPane.showMessageDialog(null,"Ja existe uma promocao nessa data ou data Inconsistente");
               }
           }
           
       });
       LimparDados.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {
               Cod.setText(null);
               Ini.setText(null);
               Fim.setText(null);
               Desc.setText(null);
           }
       });
       Sair.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               try {
                   setClosed(true);
               } catch (PropertyVetoException ex) {
                   Logger.getLogger(Aeroporto.class.getName()).log(Level.SEVERE, null, ex);
               }
           }
           
       });
       c.gridwidth=4;
       c.gridx=0;
       c.gridy=6;
       pane.add(botoes,c);
       c.gridy=4;
       c.gridwidth = 6;
       pane.add(paneVoos,c);

       this.pack();
       this.repaint();
       this.revalidate();
       
    }
}
